﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.OleDb;

namespace cn.edu.jnmc.pb
{
    /// <summary>
    ///DatabaseService 的摘要说明
    /// </summary>
    public class DatabaseService
    {
        //public static string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/App_Data/pbjnmc.mdb");

        public static string ConnStr;
        private static OleDbConnection conn; 

        public DatabaseService()
        {
            
        }

        static DatabaseService()
        {
            ConnStr = string.Format(ConfigurationManager.ConnectionStrings["PbJswsJnmc"].ConnectionString, HttpContext.Current.Server.MapPath("~/"));
            conn = null;
        }
        /// <summary>
        /// 打开数据库连接，无参数
        /// </summary>
        /// <returns></returns>
        public static OleDbConnection DBopen()
        {
            //string connStr = string.Format(ConfigurationManager.ConnectionStrings["access_url"].ConnectionString, HttpContext.Current.Server.MapPath("~/"));
            
            if(conn==null)
                conn = new OleDbConnection(ConnStr);
            if(conn.State==ConnectionState.Closed)
                conn.Open();

            if (conn.State == System.Data.ConnectionState.Open)
            {
                return conn;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 打开数据库
        /// </summary>
        /// <returns></returns>
        //public static OleDbConnection DBopen(string connStr)
        //{
        //    OleDbConnection conn = new OleDbConnection(connStr);

        //    conn.Open();

        //    if (conn.State == System.Data.ConnectionState.Open)
        //    {
        //        return conn;
        //    }
        //    else
        //    {
        //        return null;
        //    }
        //}
        /// <summary>
        /// 执行SQL,并获取数据
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="conn"></param>
        /// <returns></returns>
        public static DataSet executeQuerySQL(string sql, OleDbConnection conn){

            OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sql, conn);

            DataSet dataSet = new DataSet();

            dataAdapter.Fill(dataSet);

            return dataSet;

        }

        /// <summary>
        /// 执行SQL，获得在线查询数据
        /// </summary>
        /// <returns></returns>
        public static OleDbDataReader excuteQuerySQLOnline(string sql, OleDbConnection conn)
        {
            OleDbCommand command = new OleDbCommand(sql, conn);
            OleDbDataReader dataReader = command.ExecuteReader();

            return dataReader;
        }

        public static int executeCommendSQL(string sql, OleDbConnection conn)
        {
            int rows = 0;
            OleDbCommand command = new OleDbCommand(sql,conn);
            try
            {

                rows = command.ExecuteNonQuery();
            }
            catch (OleDbException e)
            {
                throw e;
                //e.ToString();
            }
            return rows;            
        }

        /// <summary>
        /// 关闭数据库连接
        /// </summary>
        /// <param name="conn"></param>
        public static void DBclose(OleDbConnection conn)
        {
            try
            {
                conn.Close();
            }catch(Exception e){
                e.ToString();
            }
        }
        /// <summary>
        /// 重载关闭数据库连接
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="dataReader"></param>
        public static void DBclose(OleDbConnection conn, OleDbDataReader dataReader)
        {
            try
            {
                dataReader.Close();
                conn.Close();
            }
            catch (Exception e)
            {
                e.ToString();
            }
            if (conn.State == System.Data.ConnectionState.Closed)
            {
                //Response.Write("disconnect");
            }
        }
    }
}
